//
//  CHItemButton.m
//  SZTZ
//
//  Created by 陈浩 on 2017/3/8.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHItemButton.h"
@interface CHItemButton()
/**
 *  标题内容的宽度
 */
@property (nonatomic,assign) CGFloat titleW;
/**
 *  按钮图片的尺寸
 */
@property (nonatomic,assign) CGSize imageSize;

@end
@implementation CHItemButton

-(void)awakeFromNib{
    [super awakeFromNib];
    // 计算按钮文字实际的宽度
    self.titleW = [self.currentTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.titleLabel.font} context:nil].size.width;
    
    self.imageSize = self.imageView.image.size;

    
}



- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 获得按钮本身的尺寸
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    
    // 获得按钮文字的宽度
    CGFloat titleW = self.titleLabel.frame.size.width;
    CGFloat titleH = height;
    // 计算按钮文字的x值
    CGFloat titleX = (width - titleW - self.imageView.frame.size.width - 5) * 0.5;
    
    self.titleLabel.frame = CGRectMake(titleX, 0, titleW, titleH);
    
    // 计算按钮图片的y值
    CGFloat imageY = (height - self.imageView.frame.size.height)  * 0.5;
    self.imageView.frame = CGRectMake(titleX + titleW + 5,imageY, self.imageView.frame.size.width, self.imageView.frame.size.height);
}



@end
